/*
 * tarm_iic.c
 *
 *  Created on: 2019年12月10日
 *      Author: tinnu
 */


#include "tarm_iic.h"

#include "fsl_i2c.h"

bool g_isIICInitFlag=false;

int I2C4_sendArray(uint8_t i_device7byteAddress, uint8_t i_dateAddr, const void *i_masterTxBuff, size_t i_dateNum)
{
	int32_t t_judgeVal = 0;
	if (kStatus_Success != I2C_MasterStart(I2C4_BASE, i_device7byteAddress, kI2C_Write))
		return -1;
	t_judgeVal = I2C_MasterWriteBlocking(I2C4_BASE, &i_dateAddr, 1, kI2C_TransferNoStopFlag);
	if(t_judgeVal != kStatus_Success)
		return -2;
	t_judgeVal = I2C_MasterWriteBlocking(I2C4_BASE, i_masterTxBuff, i_dateNum, kI2C_TransferDefaultFlag);
	if(t_judgeVal != kStatus_Success)
		return -3;
	t_judgeVal = I2C_MasterStop(I2C4_BASE);
	if(t_judgeVal != kStatus_Success)
		return -4;

	return 1;
}

int I2C4_readArray(uint8_t i_device7byteAddress, uint8_t i_dateAddr, void *o_masterTxBuff, size_t i_dateNum)
{
	int32_t t_judgeVal = 0;
	//start write + device addr
	if (kStatus_Success != I2C_MasterStart(I2C4_BASE, i_device7byteAddress, kI2C_Write))
		return -1;
	//write date addr
	t_judgeVal = I2C_MasterWriteBlocking(I2C4_BASE, &i_dateAddr, 1, kI2C_TransferNoStopFlag);
	if(t_judgeVal != kStatus_Success)
		return -2;
	//read restart , device addr
	t_judgeVal = I2C_MasterRepeatedStart(I2C4_BASE, i_device7byteAddress, kI2C_Read);
		if(t_judgeVal != kStatus_Success)
			return -10;
	//readd
	t_judgeVal = I2C_MasterReadBlocking(I2C4_BASE, o_masterTxBuff, i_dateNum, kI2C_TransferDefaultFlag);
	if(t_judgeVal != kStatus_Success)
		return -11;
	t_judgeVal = I2C_MasterStop(I2C4_BASE);
	if(t_judgeVal != kStatus_Success)
		return -12;

	return 1;
}

int MB_I2C4_sendArray(uint8_t i_dateAddr, const void *i_masterTxBuff, size_t i_dateNum)
{
	return I2C4_sendArray(0x5D, i_dateAddr, i_masterTxBuff, i_dateNum);
}

int MB_I2C4_readArray(uint8_t i_dateAddr, void *i_masterTxBuff, size_t i_dateNum)
{
	return I2C4_readArray(0x5D, i_dateAddr, i_masterTxBuff, i_dateNum);
}

void MB_I2C4_Init()
{

}
